/**
 * 
 */
package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import officeserver.log_error.UserException;
import officeserver.office.request.RequestException;

import GUI.helpers.ButtonType;
import GUI.helpers.PButton;
import GUI.helpers.PTextField;
import GUI.helpers.ProjectConstants;
import GUI.helpers.TextFieldType;
import GUI.main.MainClient;

/**
 * @author mramsey3
 * 
 */

public class LoginDialog extends JDialog implements ActionListener {

    private static final long serialVersionUID = 1L;
    private final static int WIDTH = 400;
    private final static int HEIGHT = 300;

    private JPanel logoPanel;
    private JPanel centerPanel;
    private JPanel userPanel;
    private JLabel userLabel;
    private PTextField userNameField;
    private JPanel passwordPanel;
    private JLabel passwordLabel;
    private JPasswordField passwordField;
    private JPanel loginPanel;
    private JLabel loginLabel;
    private JComboBox loginComboBox;
    private JPanel errorPanel;
    private JLabel errorMessage;
    private JPanel buttonPanel;
    private PButton loginButton;

    /**
     * 
     */
    public LoginDialog() {
	setSize(new Dimension(WIDTH, HEIGHT));
	final Toolkit toolkit = Toolkit.getDefaultToolkit();
	final Dimension screenSize = toolkit.getScreenSize();
	final int x = (screenSize.width - WIDTH )/ 2;
	final int y = (screenSize.height - HEIGHT) / 2;
	setLocation(x, y);
	setLayout(new BorderLayout());

	add(getLogoPanel(), BorderLayout.NORTH);

	add(getCenterPanel(), BorderLayout.CENTER);
    }

    /**
     * @return
     */
    private JPanel getLogoPanel() {
	if (logoPanel == null) {
	    logoPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("the office 2.0", 12, getHeight() - 8);
		}
	    };
	    logoPanel.setPreferredSize(new Dimension(WIDTH, ProjectConstants.HEADER_HEIGHT));
	}
	return logoPanel;
    }

    /**
     * @return
     */
    private JPanel getCenterPanel() {
	if (centerPanel == null) {
	    centerPanel = new JPanel();

	    centerPanel.setLayout(new BoxLayout(centerPanel,
		    BoxLayout.PAGE_AXIS));

	    centerPanel.add(getUserPanel());
	    centerPanel.add(getPasswordPanel());
	    centerPanel.add(getLoginPanel());
	    centerPanel.add(getErrorPanel());
	    centerPanel.add(getButtonPanel());
	}
	return centerPanel;
    }

    /**
     * @return
     */
    private JPanel getUserPanel() {
	if (userPanel == null) {
	    userPanel = new JPanel();

	    userPanel.setLayout(new BorderLayout());
	    userPanel.setBorder(new EmptyBorder(10,10,0,10));
	    
	    userPanel.add(getUserLabel(),BorderLayout.WEST);
	    userPanel.add(getUsernameField(),BorderLayout.EAST);
	}
	return userPanel;
    }

    /**
     * @return
     */
    private JLabel getUserLabel() {
	if (userLabel == null) {
	    userLabel = new JLabel(ProjectConstants.CELLPAD+"username:");
	}
	return userLabel;
    }

    /**
     * @return
     */
    private PTextField getUsernameField() {
	if (userNameField == null) {
	    userNameField = new PTextField("enter username", TextFieldType.LONG);
	}
	return userNameField;
    }

    /**
     * @return
     */
    private JPanel getPasswordPanel() {
	if (passwordPanel == null) {
	    passwordPanel = new JPanel();

	    passwordPanel.setLayout(new BorderLayout());
	    passwordPanel.setBorder(new EmptyBorder(10,10,0,10));
	    
	    passwordPanel.add(getPasswordLabel(),BorderLayout.WEST);
	    passwordPanel.add(getPasswordField(),BorderLayout.EAST);
	}
	return passwordPanel;
    }

    /**
     * @return
     */
    private JLabel getPasswordLabel() {
	if (passwordLabel == null) {
	    passwordLabel = new JLabel(ProjectConstants.CELLPAD+"password:");
	}
	return passwordLabel;
    }

    /**
     * @return
     */
    private JPasswordField getPasswordField() {
	if (passwordField == null) {
	    passwordField = new JPasswordField();
	    passwordField.setText("password");
	    passwordField.setPreferredSize(new Dimension(250,25));
	}
	return passwordField;
    }
    
    private JPanel getLoginPanel() {
	if (loginPanel == null) {
	    loginPanel = new JPanel();

	    loginPanel.setLayout(new BorderLayout());
	    loginPanel.setBorder(new EmptyBorder(10,10,0,10));
	    
	    loginPanel.add(getLoginLabel(),BorderLayout.WEST);
	    loginPanel.add(getLoginComboBox(),BorderLayout.EAST);
	}
	return loginPanel;
    }

    /**
     * @return
     */
    private JLabel getLoginLabel() {
	if (loginLabel == null) {
	    loginLabel = new JLabel(ProjectConstants.CELLPAD+"directory:");
	}
	return loginLabel;
    }
    
    private JComboBox getLoginComboBox(){
	if(loginComboBox ==  null){
	    String[] options = {"local","network"};
	    loginComboBox = new JComboBox(options);
	    loginComboBox.setPreferredSize(new Dimension(250, 25));
	    loginComboBox.setSelectedIndex(0);
	}
	return loginComboBox;
    }
    

    /**
     * @return
     */
    private JPanel getErrorPanel() {
	if (errorPanel == null) {
	    errorPanel = new JPanel();
	    errorPanel.setBorder(new EmptyBorder(5,10,0,10));
	    errorPanel.add(getErrorMessage());
	}
	return errorPanel;
    }

    /**
     * @return
     */
    private JLabel getErrorMessage() {
	if (errorMessage == null) {
	    errorMessage = new JLabel(ProjectConstants.CELLPAD);
	}
	return errorMessage;
    }

    /**
     * @return
     */
    private JPanel getButtonPanel() {
	if (buttonPanel == null) {
	    buttonPanel = new JPanel();
	    
	    buttonPanel.setBorder(new EmptyBorder(5,10,10,10));
	    buttonPanel.add(getLoginButton());
	}
	return buttonPanel;
    }

    /**
     * @return
     */
    private PButton getLoginButton() {
	if (loginButton == null) {
	    loginButton = new PButton("login",ButtonType.MEDIUM);
	    loginButton.addActionListener(this);
	}
	return loginButton;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == getLoginButton()) {
	    if(getLoginComboBox().getSelectedItem() == "local"){
		MainClient.setIsLocal(true);
	    }else{
		MainClient.setIsLocal(false);
	    }
	    try {
		System.out.println(String.valueOf(getPasswordField()
			.getPassword()));
		MainClient.getConnection().userLogin(
			getUsernameField().getText(),
			String.valueOf(getPasswordField().getPassword()));
		try {
		    if (MainClient.getConnection().isLoggedIn()) {
			this.setVisible(false);
			MainClient.setLoggedIn(true);
		    }
		} catch (Exception e1) {
		    e1.printStackTrace();
		}
	    } catch (UserException e1) {
		getErrorMessage().setText("Wrong User/Password Combination.");
		e1.printStackTrace();
	    }
		catch (RequestException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
	}
    }
}
